package com.dreamana.components
{
	import com.dreamana.ui.BaseComponent;
	import com.dreamana.ui.ISkinnable;
	import com.dreamana.ui.SkinGraphics;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Rectangle;
	
	
	public class Button extends BaseComponent implements ISkinnable
	{
		protected var _back:Shape;
		protected var _backColor:int = -1;
		protected var _backTexture:BitmapData;
		protected var _backScale9Grid:Rectangle;
		protected var _label:DisplayObject;
		
		public function Button(w:int, h:int, label:DisplayObject=null)
		{
			_width = w;
			_height = h;
			_label = label;
			
			//default setting
			this.buttonMode = true;
			
			this.init();
		}
	
		override protected function addChildren():void
		{
			_back = new Shape();
			this.addChild(_back);
			
			if(_label) this.addChild(_label);
			
			this.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
		}
		
		//--- PUBLIC METHODS ---
		
		override public function draw():void
		{
			var g:Graphics = _back.graphics;
			
			g.clear();
			
			if(_backTexture) {
				if(_backScale9Grid) SkinGraphics.fill9Grid(g, _backTexture, _width, _height, _backScale9Grid, false);
				else SkinGraphics.fillRect(g, _backTexture, _width, _height);
			}
			else if(_backColor >= 0) {
				SkinGraphics.drawBlank(g, _width, _height, _backColor);
			}
			else {
				SkinGraphics.drawWireFrame(g, _width, _height);
			}
		}
		
		override public function setStyle(style:String, value:Object):void
		{
			super.setStyle(style, value);
			
			switch(style) {
				case "background-color":
					_backColor = value as Number;
					invalidate();
					break;
				case "background-image":
					_backTexture = value as BitmapData;
					invalidate();
					break;
				case "background-9grid":
					_backScale9Grid = value as Rectangle;
					invalidate();
					break;
			}
		}
		
		//--- EVENT HANDLERS ---
		
		protected var c0:ColorTransform = new ColorTransform();
		protected var c1:ColorTransform = new ColorTransform(1,1,1,1, 64,64,64,0);
		
		protected function onDown(e:MouseEvent):void
		{
			stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
			this.transform.colorTransform = c1;
		}
		
		protected function onUp(e:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
			this.transform.colorTransform = c0;
		}
		
		//--- GETTER/SETTERS ---
		public function get background():Shape { return _back; }
		
		public function get label():DisplayObject { return _label; }
		public function set label(value:DisplayObject):void
		{
			if(_label && this.contains(_label)) this.removeChild(_label);
			
			_label = value;
			this.addChild(_label);
		}
		
		protected var f0:Array = [];
		protected var f1:Array = [new ColorMatrixFilter([0.3086,0.6094,0.082,0,0,0.3086,0.6094,0.082,0,0,0.3086,0.6094,0.082,0,0,0,0,0,1,0])];
				
		public override function set enabled(value:Boolean):void
		{
			super.enabled = value;
			
			this.buttonMode = value;
			this.filters = value? f0 : f1;
		}
	}
}